package com.yc.aspect;

import com.yc.domain.JsonModel;
import com.yc.domain.Resfood;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Order(10)
@Slf4j
public class DetailCountAspect {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    private String countKey="detailCount";
    @Pointcut("execution(* com.yc.controller.ResfoodController.findById(..))")
    private void deposit(){}

    @Around("deposit()")
    public Object count(ProceedingJoinPoint pjp) throws Throwable {
        //取出参数
        Object[] args = pjp.getArgs();
        String fid= args[0].toString();
        //根据key的值自增1
        long count=redisTemplate.opsForValue().increment(countKey+fid);

        JsonModel jm = (JsonModel) pjp.proceed();
        if(jm.getCode()==1){
            Resfood obj = (Resfood) jm.getObj();
            obj.setDetailCount(count);
        }
        return jm;
    }
}
